﻿using mars_server.csharp.model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace mars_server.csharp.socket
{
    public class UserSocket
    {

        private Object lockHbOb = new Object();//hb锁对象

        private Object lockBwOb = new Object();//bw锁对象

        private Object lockOpOb = new object();//isOp锁对象

        public BinaryReader br { get;  set; }

        private BinaryWriter bw;

        private volatile int heartBeat;//心跳数据

        private volatile bool isOp;//该UserSocket对象是否进行读取操作
        public UserDO userDO { get; set; }

        public TcpClient client {get; set;}

        
        public UserSocket(TcpClient client)
        {       
            this.HeartBeat = 5;
            this.client = client;
            IsOp = false;
            Console.WriteLine("client "+client.Client.RemoteEndPoint.ToString()+" 已经建立连接");
            NetworkStream networkStream = client.GetStream();
            br = new BinaryReader(networkStream);
            Bw = new BinaryWriter(networkStream);
        }


        public int HeartBeat
        {
            get
            {
                return heartBeat;
            }
            set
            {
                lock (lockHbOb)
                {
                    heartBeat = value;
                }
            }
        }//同步锁防止同时进行get和set心跳数据

        public BinaryWriter Bw
        {
            get
            {
                lock (lockBwOb)
                {
                    return bw;
                }
            }
            set
            {
                lock (lockBwOb)
                {
                    bw = value;
                }
            }
        }//同步锁防止同时进行写同一个客户端写操作

        public bool IsOp
        {
            get
            {
                return isOp;
            }

            set
            {
                lock (lockOpOb)
                {
                    isOp = value;
                }
            }
        }//同步锁防止同时进行写同一个客户端操作该对象 

    }
}
